﻿using BLL;
using BLL.Access;
using Common;
using Entity;
using Platform.Permission;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web
{
    public partial class OALogin : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {

            if(string.IsNullOrWhiteSpace(Request["url"])==false
                && string.IsNullOrWhiteSpace(Request["userId"]) == false
                 && string.IsNullOrWhiteSpace(Request["pwd"]) == false
                  && string.IsNullOrWhiteSpace(Request["wtype"]) == false)
            {
                try
                {
                    Redirect(Request["url"], Request["userId"], Request["pwd"], Request["wtype"]);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        private void Redirect(string url,string userName, string userPassword,string WType)
        {
            var encryptAndDecrypt = new EncryptAndDecrypt();
            var encryptPass = encryptAndDecrypt.Encrypt(userPassword.Trim());
            if (LoginHelper.CheckPassword(userName.Trim(), encryptPass))
            {
                var hck = new HttpCookie("PlanWater");
                hck.Values.Set("name", userName.Trim());
                hck.Values.Set("pwd", userPassword.Trim());
                hck.Expires = DateTime.Now.AddYears(1);
                Response.AppendCookie(hck);
                LoginHelper.SaveUserProfile(userName.Trim());
                var rsno = "-1";
                var rsmsg = "";
                var UserId = LoginHelper.GetUserInfo(userName.Trim()).UserID;
                //获取当前登录人电子签名
                var dtT = BusinessTool.GetTextAndValue("AS_USERINFO", "USERID", "S_DZQM", null, "USERID=" + UserId)
                    .Tables[0];
                LoginInfo.CurrentUser.ElectronSign = Convert.ToString(dtT.Rows[0]["VALUE"]);
                new SpHelper().P_LOGIN(UserId, 1, out rsno, out rsmsg);
                if (rsno == "-1")
                {
                    throw new Exception(rsmsg); 
                }

                //BLL.BusinessTool.WType = Convert.ToInt32(this.ItemsW.Value.ToString());////drpWaterItem.SelectedValue
                Session["WType"] = WType;
                if (null == HttpContext.Current.Session["_UserName"])
                {
                    HttpContext.Current.Session["_UserName"] = Platform.Permission.LoginHelper.Name;
                }
                Response.Redirect(Request["url"]);
            }
        }

    }
}